<!--
 * @author luguoxiang
 * @date 2022/9/17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <edit-container ref="containerRef" v-bind="containerConfig">
    <lg-form ref="formRef" v-loading="loading" :schema="schema" v-bind="formConfig">
      <template #component="{ data }">
        <t-auto-complete
          v-model="data.component"
          style="width: 100%"
          :options="options"
          highlight-keyword
          placeholder="请输入页面组件"
          clearable
        />
      </template>
    </lg-form>
  </edit-container>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { EditContainer, useEditContainer } from '@/components';
import { LgForm } from '@/soar';
import schema from './schema';
import api from './api';

const emits = defineEmits(['ok']);

const components = [];
const containerRef = ref();
const formRef = ref();
const options = ref([]);

function onChange(value) {
  options.value = components.filter((x) => x.indexOf(value));
}

const { containerConfig, formConfig, loading, open } = useEditContainer<any>({
  header: '租户',
  containerRef,
  formRef,
  getData: (params: any) => api.detail(params.id),
  onOk: async ({ values, data, params, isUpdate, changed }) => {
    if (changed) {
      values.id = data.id;
      await api.saveOrUpdate(values, isUpdate);
      emits('ok', values);
    }
  },
  containerProps: {
    width: '600px',
  },
  onOpen(params: any) {
    const modules = import.meta.glob('@/pages/**/*.{vue,tsx}');
    const layouts = import.meta.glob('@/layouts/*.{vue,tsx}');
    const list = Object.keys(modules).map((x) => `pages/${x.substring(6)}`);
    Object.keys(layouts).forEach((x) => list.push(x.substring(9)));
    options.value = list;
  },
  data: { type: 1, status: 0 },
});

defineExpose({ open });
</script>
